package com.harmontronics.hanma.paas.paas.controller;

import com.harmontronics.hanma.paas.paas.annotation.TokenValidSkip;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ldb
 */
@RequestMapping("/api")
@RestController
@Api(tags = "发布后的APP相关接口")
@ConditionalOnMissingClass("com.harmontronics.hanma.paas.paas.interceptor.TokenValidInterceptor")
public class HmAppPublishedController {


    @Value("${app.name}")
    private String appName;


    @GetMapping("/tenant/app/info-unique")
    @ApiOperation("发布后的APP信息")
    @TokenValidSkip
    public ResponseEntity<Map<String,Object>> appInfo(){
        Map<String,Object> infoMap=new HashMap<>();
        infoMap.put("appName",appName);
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File homeDir=applicationHome.getDir();
        File logoFile = new File(homeDir,"logo.txt");
        if(!logoFile.exists()){
            infoMap.put("icon","");
        }else{
            try {
                String logoStr = new String(Files.readAllBytes(logoFile.toPath()));
                infoMap.put("icon",logoStr);
            }catch (IOException ex){
                ex.printStackTrace();
                infoMap.put("icon","");
            }

        }
        return ResponseEntity.ok(infoMap);
    }
}
